package com.gcsoftware.web.mapper;

import com.gcsoftware.web.beans.T_GW_INTERFS;
import com.gcsoftware.web.beans.T_GW_INTERF_PARAMS;
import com.gcsoftware.web.beans.T_CON_DSITEM_IPARAMS;
import com.gcsoftware.web.beans.response.T_GW_INTERFS_RSP;
import com.gcsoftware.web.mapper.providers.InterfsProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by qs on 2018/3/29.
 */
@Mapper
public interface T_GW_INTERFMAPPER {


    @Select("<script>select " +
            "C_INTERF_ID  ,c_INTERF_NAMEP,C_INTERF_NAME ,C_INTERF_PROTOCOL ,C_INTERF_METHOD_NAME ,C_INTERF_HTTP_METHOD ," +
            "C_INTERF_ENDPOINT ,to_char(itfs.D_CREATE_TIME , 'yyyy-MM-dd hh24:mi:ss') c_CREATE_TIME ," +
            "C_INTERF_REMARK ,C_INTERF_STATUS ,itfs.C_APP_ID,app.C_APP_NAME  ,C_INTERF_UNIFY ,C_INTERF_SERVICE ," +
            "C_IPARAMS_OUT_TYPE ,itfs.c_INTERF_STATUS,dict.c_DICT_ITEM_NAME c_INTERF_STATUS_NAME " +
            "from t_gw_interfs itfs , t_gw_apps app ,t_COM_SYS_DICTITEM dict " +
            "where app.C_APP_ID  = itfs.C_APP_ID and dict.c_DICT_ITEM_VALUE=itfs.c_INTERF_STATUS " +
            "<when test='null == c_COMPANY_ID'>AND itfs.c_INTERF_STATUS != 'INTERF_STATUS_INIT' </when>" +
            "<when test='null != c_COMPANY_ID'>AND app.c_COMPANY_ID=#{c_COMPANY_ID} </when>" +
            "<when test='null != c_APP_ID'>AND itfs.c_APP_ID=#{c_APP_ID} </when>" +
            "<when test='null != c_INTERF_STATUS'>AND itfs.c_INTERF_STATUS=#{c_INTERF_STATUS} </when>" +
            "<when test='null != c_INTERF_NAME'>AND itfs.c_INTERF_NAME like concat(concat('%',#{c_INTERF_NAME}) , '%') </when>" +
            "</script>")
    public List<T_GW_INTERFS> queryInterfsForList(@Param(value = "c_APP_ID") String c_APP_ID,@Param(value = "c_INTERF_STATUS") String c_INTERF_STATUS,@Param(value = "c_INTERF_NAME") String c_INTERF_NAME,@Param(value = "c_COMPANY_ID") String c_COMPANY_ID);


    @Select("SELECT max(C_INTERF_ID) FROM T_GW_INTERFS ")
    public String getMaxId();

    @Select("<script>" +
            "SELECT " +
            "C_INTERF_ID  ,c_INTERF_NAMEP,C_INTERF_NAME ,C_INTERF_PROTOCOL ,C_INTERF_METHOD_NAME ,C_INTERF_HTTP_METHOD ," +
            "C_INTERF_ENDPOINT ,to_char(itfs.D_CREATE_TIME , 'yyyy-MM-dd hh24:mi:ss') c_CREATE_TIME ," +
            "C_INTERF_REMARK ,C_INTERF_STATUS ,itfs.C_APP_ID,app.C_APP_NAME  ,C_INTERF_UNIFY ,C_INTERF_SERVICE ," +
            "C_IPARAMS_OUT_TYPE " +
            "from t_gw_interfs itfs , t_gw_apps app " +
            "where app.C_APP_ID  = itfs.C_APP_ID " +
            "<when test='null != c_INTERF_ID'>AND c_INTERF_ID=#{c_INTERF_ID} </when>" +
            "<when test='null != c_INTERF_NAME'>AND c_INTERF_NAME=#{c_INTERF_NAME} </when>" +
            "</script>"
    )
    public T_GW_INTERFS get(@Param(value = "c_INTERF_ID") String c_INTERF_ID, @Param(value = "c_INTERF_NAME") String c_INTERF_NAME);

    @Select("<script>" +
            "SELECT " +
            "C_INTERF_ID  ,c_INTERF_NAMEP,C_INTERF_NAME ,C_INTERF_PROTOCOL ,C_INTERF_METHOD_NAME ,C_INTERF_HTTP_METHOD ," +
            "C_INTERF_ENDPOINT ,to_char(itfs.D_CREATE_TIME , 'yyyy-MM-dd hh24:mi:ss') c_CREATE_TIME ," +
            "C_INTERF_REMARK ,C_INTERF_STATUS ,itfs.C_APP_ID,app.C_APP_NAME  ,C_INTERF_UNIFY ,C_INTERF_SERVICE ," +
            "C_IPARAMS_OUT_TYPE " +
            "from t_gw_interfs itfs , t_gw_apps app " +
            "where app.C_APP_ID  = itfs.C_APP_ID " +
            "<when test='null != c_INTERF_ID'>AND c_INTERF_ID=#{c_INTERF_ID} </when>" +
            "<when test='null != c_INTERF_NAME'>AND c_INTERF_NAME=#{c_INTERF_NAME} </when>" +
            "</script>"
    )
    public T_GW_INTERFS_RSP getForRsp(@Param(value = "c_INTERF_ID") String c_INTERF_ID, @Param(value = "c_INTERF_NAME") String c_INTERF_NAME);


    @Insert("INSERT INTO t_gw_interfs(C_INTERF_ID ,C_INTERF_NAME ,C_INTERF_PROTOCOL ,c_INTERF_NAMEP,C_INTERF_METHOD_NAME ,C_INTERF_HTTP_METHOD ," +
            "C_INTERF_ENDPOINT ,C_INTERF_REMARK ,C_INTERF_STATUS ,C_APP_ID,C_INTERF_UNIFY ,C_INTERF_SERVICE ,C_IPARAMS_OUT_TYPE)" +
            "VALUES(#{c_INTERF_ID} , #{c_INTERF_NAME} , #{c_INTERF_PROTOCOL} , #{c_INTERF_NAMEP} , #{c_INTERF_METHOD_NAME} , #{c_INTERF_HTTP_METHOD} ," +
            "#{c_INTERF_ENDPOINT} , #{c_INTERF_REMARK} , #{c_INTERF_STATUS} , #{c_APP_ID} , #{c_INTERF_UNIFY} , #{c_INTERF_SERVICE} , #{c_IPARAMS_OUT_TYPE})")
    public int insertInterf(T_GW_INTERFS interf);

    @Update("<script>UPDATE T_GW_INTERFS SET D_MODIFY_TIME = CURRENT_TIMESTAMP() " +
            "<when test='null != c_INTERF_NAME'>,c_INTERF_NAME=#{c_INTERF_NAME} </when> " +
            "<when test='null != c_INTERF_PROTOCOL'>,c_INTERF_PROTOCOL=#{c_INTERF_PROTOCOL} </when> " +
            "<when test='null != c_INTERF_NAMEP'>,c_INTERF_NAMEP=#{c_INTERF_NAMEP} </when> " +
            "<when test='null != c_INTERF_METHOD_NAME'>,c_INTERF_METHOD_NAME=#{c_INTERF_METHOD_NAME} </when> " +
            "<when test='null != c_INTERF_HTTP_METHOD'>,c_INTERF_HTTP_METHOD=#{c_INTERF_HTTP_METHOD} </when> " +
            "<when test='null != c_INTERF_ENDPOINT'>,c_INTERF_ENDPOINT=#{c_INTERF_ENDPOINT} </when> " +
            "<when test='null != c_INTERF_REMARK'>,c_INTERF_REMARK=#{c_INTERF_REMARK} </when> " +
            "<when test='null != c_INTERF_STATUS'>,c_INTERF_STATUS=#{c_INTERF_STATUS} </when> " +
            "<when test='null != c_APP_ID'>,c_APP_ID=#{c_APP_ID} </when> " +
            "<when test='null != c_INTERF_UNIFY'>,c_INTERF_UNIFY=#{c_INTERF_UNIFY} </when> " +
            "<when test='null != c_INTERF_SERVICE'>,c_INTERF_SERVICE=#{c_INTERF_SERVICE} </when> " +
            "<when test='null != c_IPARAMS_OUT_TYPE'>,c_IPARAMS_OUT_TYPE=#{c_IPARAMS_OUT_TYPE} </when> " +
            "WHERE c_INTERF_ID = #{c_INTERF_ID} " +
            "</script>")
    public int updateInterf(T_GW_INTERFS interf);

    @Delete("DELETE T_GW_INTERFS WHERE c_INTERF_ID  = #{c_INTERF_ID} AND c_INTERF_STATUS != 'INTERF_STATUS_PUBLISH'")
    public int deleteInterf(String c_INTERF_ID);

    @Delete("DELETE T_GW_INTERF_PARAMS WHERE c_INTERF_ID = #{c_INTERF_ID}")
    public int deleteIparams(String c_INTERF_ID);

    @Insert("INSERT INTO T_GW_INTERF_PARAMS(c_IPARAMS_ID,c_INTERF_ID,c_IPARAMS_NAME,c_IPARAMS_REMARK,c_IPARAMS_SERVICE,c_IPARAMS_DATATYPE)" +
            "VALUES (#{c_IPARAMS_ID},#{c_INTERF_ID},#{c_IPARAMS_NAME},#{c_IPARAMS_REMARK},#{c_IPARAMS_SERVICE},#{c_IPARAMS_DATATYPE})")
    public int insertIParams(T_GW_INTERF_PARAMS iparam);

    @Update("UPDATE T_GW_INTERF_PARAMS SET D_MODIFY_TIME = CURRENT_TIMESTAMP(),c_IPARAMS_REMARK=#{c_IPARAMS_REMARK},c_IPARAMS_SERVICE=#{c_IPARAMS_SERVICE},c_IPARAMS_DATATYPE=#{c_IPARAMS_DATATYPE} WHERE c_IPARAMS_ID = #{c_IPARAMS_ID}")
    public int updateIParams(T_GW_INTERF_PARAMS iparam);

    @Select("select c_IPARAMS_ID,c_INTERF_ID,c_IPARAMS_NAME,c_IPARAMS_REMARK,c_IPARAMS_SERVICE,c_IPARAMS_DATATYPE,dt.c_DICT_ITEM_NAME c_IPARAMS_DATATYPE_NAME from t_GW_INTERF_PARAMS prm,t_com_sys_dictitem dt WHERE dt.c_DICT_ITEM_VALUE=prm.c_IPARAMS_DATATYPE and c_INTERF_ID = #{c_INTERF_ID}")
    public List<T_GW_INTERF_PARAMS> queryIparamsList(String c_INTERF_ID);


    @Select("SELECT max(c_IPARAMS_ID) FROM T_GW_INTERF_PARAMS ")
    public String getMaxIparamId();

    @Select("select c_IPARAMS_ID,c_INTERF_ID,c_IPARAMS_NAME,c_IPARAMS_REMARK,c_IPARAMS_SERVICE,c_IPARAMS_DATATYPE from t_GW_INTERF_PARAMS WHERE c_IPARAMS_ID = #{c_IPARAMS_ID}")
    public T_GW_INTERF_PARAMS getIParamsById(String c_IPARAMS_ID);

    @Select("<script>SELECT " +
            "ipr.c_IPARAMS_ID,ipr.c_INTERF_ID, cdi.c_DS_ITEM_ID,ipr.c_IPARAMS_NAME,ipr.c_IPARAMS_REMARK, case when cdi.c_IPARAMS_ID is not null then 1 else 0 end ifChecked,cdi.c_IPARAMS_SENS  " +
            "FROM T_GW_INTERF_PARAMS  ipr " +
            "left join T_CON_DSITEM_IPARAMS  cdi on cdi.C_INTERF_ID  = ipr.C_INTERF_ID  and cdi.C_IPARAMS_ID  = ipr.C_IPARAMS_ID <when test='null != c_DS_ITEM_ID'> and cdi.c_DS_ITEM_ID = #{c_DS_ITEM_ID}</when>" +
            "WHERE ipr.c_INTERF_ID = #{c_INTERF_ID} </script>")
    public List<T_CON_DSITEM_IPARAMS> getIParamsRsp(
            @Param(value = "c_DS_ITEM_ID") String c_DS_ITEM_ID,
            @Param(value = "c_INTERF_ID") String c_INTERF_ID);

    @Update("UPDATE T_GW_INTERFS SET D_MODIFY_TIME = CURRENT_TIMESTAMP(),C_INTERF_STATUS =#{c_INTERF_STATUS} WHERE c_INTERF_ID = #{c_INTERF_ID}")
    public int updateInterfStatus(@Param("c_INTERF_ID") String c_INTERF_ID,@Param("c_INTERF_STATUS") String c_INTERF_STATUS);


    @Select("select " +
                "cri.c_INTERF_ID,cri.c_IPARAMS_ID,c_IPARAMS_NAME,c_IPARAMS_SERVICE,case when cdi.c_IPARAMS_ID is not null then 0 else 1 end isabled,C_IPARAMS_SENS,c_IPARAMS_DATATYPE " +
            "from (" +
                "SELECT " +
                    "ipr.c_INTERF_ID,c_IPARAMS_ID,c_IPARAMS_NAME,ipr.C_IPARAMS_DATATYPE ,C_IPARAMS_SERVICE  " +
                "FROM " +
                    "T_CON_ROLE_INTERFS cri,T_GW_INTERF_PARAMS ipr " +
                "WHERE " +
                    "ipr.C_INTERF_ID = cri.C_INTERF_ID " +
                    "AND c_ROLE_ID=#{c_ROLE_ID} " +
                ") cri " +
            "LEFT JOIN ( " +
                "SELECT * FROM T_CON_DSITEM_IPARAMS cdi " +
                "WHERE  EXISTS (" +
                    "SELECT 1 FROM T_CON_ROLE_DSITEM dsi " +
                    "WHERE " +
                    "dsi.C_ROLE_ID=#{c_ROLE_ID} " +
                    "AND dsi.c_DS_ITEM_ID = cdi. C_DS_ITEM_ID  " +
                ") " +
            ") cdi " +
            "on " +
            "cdi.c_INTERF_ID  = cri.c_INTERF_ID " +
            "and cdi.c_IPARAMS_ID = cri.c_IPARAMS_ID")
    public List<T_GW_INTERF_PARAMS> queryIparamsForRole(@Param(value = "c_ROLE_ID") String c_ROLE_ID);

    @DeleteProvider(type=InterfsProvider.class,method = "batchDelIparams")
    public int batchDeleteIparams(@Param(value = "iparamsList") List<T_GW_INTERF_PARAMS> iparamsList);
}
